 /**
 * 逻辑、功能相关描述:物品托管
 *
 * @author yandawei 编写日期：2009-1-8
 */
package com.ce.pms.occupation.web;

import java.util.Collections;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.service.IClientRHouseService;
import com.ce.pms.house.model.CoalitionHouseDomain;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.occupation.service.IThingHandoverService;
import com.ce.pms.occupation.vo.ThingEntrustQueryConditionVO;
import com.ce.pms.occupation.vo.ThingEntrustVO;
import com.ce.pms.occupation.vo.ThingGivebackVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * @author yandawei
 *
 */
public class ThingEntrustAction extends PmsBaseAction {

	private IHouseService houseService;

	private IClientRHouseService clientRHouseService;

	private IThingHandoverService thingHandoverService;

	private IBuildingService buildingService;



	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * 功能描述: 物品托管主界面 <br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingEntrustMain(){
//		楼栋列表
		List<BuildingVO> buildList = buildingService.getAllBuildingList();
		Collections.sort(buildList, PmsUtilityComparator.getComparator());
		setAttribute("buildList", buildList);
		return "thingEntrustMain";
	}

	/**
	 * 功能描述:  物品托管操作界面<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingEntrustBaseMain(){
		return "thingEntrustBaseMain";
	}

	/**
	 * 功能描述:  设置查询条件到缓存<br>
	 * @param queryConditionVO
	 * Create author:yandawei <br>
	 * Create on:2009-1-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void setThingEntrustQueryCondition2Cache(@Read ThingEntrustQueryConditionVO queryConditionVO){
		setAttribute("condition", queryConditionVO, Scope.SESSION);
	}

	/**
	 * 功能描述:  获取缓存中的查询条件<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ThingEntrustQueryConditionVO getThingEntrustQueryConditionFromCache(){
		ThingEntrustQueryConditionVO obj = (ThingEntrustQueryConditionVO) getAttribute("condition",Scope.SESSION);
		return obj;
	}

	/**
	 * 功能描述:  初始托管列表<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingEntrustList(){
		setThingEntrustQueryCondition2Cache(new ThingEntrustQueryConditionVO());
		setAttribute("totalRows",new Integer(0));
		return "thingEntrustList";
	}
	/**
	 * 功能描述:  物品托管列表<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String thingEntrustList(
			@Read(key="isShowMenu",defaultValue="true")@Out(key="isShowMenu")Boolean isShowMenu,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "10")Integer pageSize
			){
		ThingEntrustQueryConditionVO queryConditionVO = getThingEntrustQueryConditionFromCache();
		queryConditionVO.setPageNo(pageNO);
		queryConditionVO.setPageSize(pageSize);
		Page page = thingHandoverService.queryThingEntrustList(queryConditionVO);
		if(!BlankUtil.isBlank(page)){
			setAttribute("thingEntrustList", page.getResult());
			setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		}else{
			setAttribute("totalRows",new Integer(0));
		}
		return "thingEntrustList";
	}

	/**
	 * 功能描述:  初始化物品托管新增界面<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingEntrustAdd(){
		setAttribute("owners", null);
		setAttribute("flag", "add");
		return "thingEntrustEdit";
	}

	/**
	 * 功能描述:  根据房间ID查询带出业主与管理房号(格式:{houseId:房间ID,coalitionHouseNumber:管理房号,ownerName:"业主1,业主2"})<br>
	 * @param houseId
	 * Create author:yandawei <br>
	 * Create on:2009-1-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void  queryOwnerHouseInfo(@Read(key="houseId")Long houseId){
		CoalitionHouseDomain coalitionHouseDomain =	houseService.getCoalition(houseId);
    	List<ParticipantDO> list =  clientRHouseService.getAllOwnerByHouseId(houseId);
    	if(BlankUtil.isBlank(list)){
    		this.renderText("false");
    	}else{
	    	StringBuffer jsonStr = new StringBuffer("{");
	    	jsonStr.append("\"houseId\" : \""+ houseId +"\",");
			jsonStr.append("\"coalitionHouseNumber\" : \"");
			if(!BlankUtil.isBlank(coalitionHouseDomain)){
				jsonStr.append(coalitionHouseDomain.getHouseNumber());
			}
			jsonStr.append("\",");
			jsonStr.append("\"ownerName\" :\"");
			for(ParticipantDO participantDO : list){
				jsonStr.append(participantDO.getParticipantName()).append(",");
			}
			jsonStr = jsonStr.deleteCharAt(jsonStr.length()-1);
			jsonStr.append("\"}");
			this.renderText(jsonStr.toString());
    	}
	}

	/**
	 * 功能描述:  托管修改初始化时根据房间ID查询业主信息<br>
	 * @param thingEntrustVO
	 * Create author:yandawei <br>
	 * Create on:2009-1-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void setClinetInfo(ThingEntrustVO thingEntrustVO){
		CoalitionHouseDomain coalitionHouseDomain =	houseService.getCoalition(thingEntrustVO.getHouseId());
    	List<ParticipantDO> list =  clientRHouseService.getAllOwnerByHouseId(thingEntrustVO.getHouseId());
    	StringBuffer sb = new StringBuffer("");
		if(!BlankUtil.isBlank(coalitionHouseDomain)){
			thingEntrustVO.setCoalitionHouseNumber(coalitionHouseDomain.getHouseNumber());
		}

		for(ParticipantDO participantDO : list){
			sb.append(participantDO.getParticipantName()).append(",");
		}
		sb = sb.deleteCharAt(sb.length()-1);
		setAttribute("owners", sb.toString());
	}

	/**
	 * 功能描述:  新增物品托管<br>
	 * @param thingEntrustVO
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void addThingEntrust(@Read ThingEntrustVO thingEntrustVO){
		thingHandoverService.addThingEntrust(thingEntrustVO);
		this.renderText("true");
	}

	/**
	 * 功能描述: 初始化物品托管修改界面 <br>
	 * @param id
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingEntrustEdit(@Read(key="id")Long thingEntrustId){
		ThingEntrustVO thingEntrustVO = thingHandoverService.getThingEntrustInfo(thingEntrustId);
		setClinetInfo(thingEntrustVO);
		setAttribute("flag", "edit");
		setAttribute("thingEntrust", thingEntrustVO);
		return "thingEntrustEdit";
	}

	/**
	 * 功能描述:  检查是否可能修改托管记录<br>
	 * @param thingEntrustId
	 * Create author:yandawei <br>
	 * Create on:2009-1-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkThingEntrustEdit(@Read(key="id")Long thingEntrustId){
		boolean result = thingHandoverService.isAllowEditThingEntrust(thingEntrustId);
		if(result){
			this.renderText("true");
		}else{
			this.renderText("false");
		}
	}

	/**
	 * 功能描述:  修改物品托管<br>
	 * @param thingEntrustVO
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void editThingEntrust(@Read ThingEntrustVO thingEntrustVO){
		thingHandoverService.editThingEntrust(thingEntrustVO);
		this.renderText("true");
	}

	/**
	 * 功能描述:  检查是否可以删除托管记录<br>
	 * @param thingEntrustId
	 * Create author:yandawei <br>
	 * Create on:2009-1-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkThingEntrustDelete(@Read(key="id")Long thingEntrustId){
		boolean result = thingHandoverService.isAllowDeleteThingEntrust(thingEntrustId);
		if(result){
			this.renderText("true");
		}else{
			this.renderText("false");
		}
	}

	/**
	 * 功能描述:  删除物品托管<br>
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void deleteThingEntrust(@Read(key="id")Long thingEntrustId){
		thingHandoverService.deleteThingEntrust(thingEntrustId);
		this.renderText("true");
	}

	/**
	 * 功能描述: 物品托管详细信息 <br>
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2009-1-9  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String getThingEntrustDetail(@Read(key="id")Long thingEntrustId){
		//业主与房间信息
		ThingEntrustVO thingEntrustVO = thingHandoverService.getThingEntrustInfo(thingEntrustId);
		CoalitionHouseDomain coalitionHouseDomain =	houseService.getCoalition(thingEntrustVO.getHouseId());
		if(BlankUtil.isBlank(coalitionHouseDomain)){
			thingEntrustVO.setCoalitionHouseNumber(coalitionHouseDomain.getHouseNumber());
		}
    	List<ParticipantDO> list =  clientRHouseService.getAllOwnerByHouseId(thingEntrustVO.getHouseId());
    	StringBuffer str = new StringBuffer("");
		for(ParticipantDO participantDO : list){
			str.append(participantDO.getParticipantName()).append(",");
		}
		str = str.deleteCharAt(str.length()-1);
		setAttribute("owners", str);
		ThingGivebackVO thingGivebackVO =  thingHandoverService.getThingGivebackByThingEntrustId(thingEntrustId);
		setAttribute("thingEntrust", thingEntrustVO);
		setAttribute("thingGiveback", thingGivebackVO);
		return "thingEntrustDetail";
	}

	/**
	 * 功能描述:  初始化物品归还界面<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initThingGiveback(@Read(key="thingEntrustId")Long id){
		ThingGivebackVO thingGivebackVO =  thingHandoverService.getThingGivebackByThingEntrustId(id);
		ThingEntrustVO thingEntrustVO = thingHandoverService.getThingEntrustInfo(id);

		setAttribute("thingGiveback", thingGivebackVO);
		setAttribute("thingEntrustId", id);
		setAttribute("acceptDate", DateUtil.convertDateToStr(thingEntrustVO.getAcceptDate(), "yyyy-MM-dd"));
		return "thingGivebackEdit";
	}

	/**
	 * 功能描述:  新增物品归还<br>
	 * @param givebackVO
	 * Create author:yandawei <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void addThingGiveback(@Read ThingGivebackVO givebackVO){
		thingHandoverService.handleThingGiveback(givebackVO);
		this.renderText("true");
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}


	public void setClientRHouseService(IClientRHouseService clientRHouseService) {
		this.clientRHouseService = clientRHouseService;
	}

	public void setThingHandoverService(IThingHandoverService thingHandoverService) {
		this.thingHandoverService = thingHandoverService;
	}


}
